﻿<script type="text/javascript">
    $(function () {
        getButton("paradise");
    });

    var oldParadiseId = 0;

    function ui_paradise_init_list(toolbar) {
        
        $("#ui_paradise_dg").datagrid({       //初始化datagrid
            url: "ashx/bg_paradise.ashx?action=search",
            striped: true, rownumbers: true, pagination: true, pageSize: 20, singleSelect: true,
            idField: 'id',
            sortName: 'code',
            sortOrder: 'asc',
            pageList: [20, 40, 60, 80, 100],
            frozenColumns: [[
                { field: 'code', title: '编码', sortable: true, width: 40 },
                { field: 'name', title: '名称', sortable: true, width: 80 }
            ]],
            columns: [[
                       { field: 'paradiseNum', title: '序号', sortable: true, width: 60 },
                       { field: 'fullName', title: '全名', sortable: true, width: 150 },
                       { field: 'type', title: '类型', sortable: true, width: 150 },
                       { field: 'userNames', title: '管理员', width: 100 },
                       { field: 'areaName', title: '所属区域', sortable: true, width: 80 },
                       //{ field: 'floors', title: '楼层数',  width: 80 },
                       //{ field: 'eastSections', title: '东区域数', width: 80 },
                       //{ field: 'westSections', title: '西区域数', width: 80 },
                       //{ field: 'lines', title: '每区域排数', width: 80 },
                       //{ field: 'layers', title: '每排层数', width: 80 },
                       //{ field: 'columns', title: '每层个数', width: 80 },
                       { field: 'graves', title: '容量', width: 80 },
                       { field: 'crdate', title: '创建日期', sortable: true, width: 150 }
            ]],
            toolbar: toolbar.length == 0 ? null : toolbar,   //请求回来的当前用户可以操作的按钮
            onDblClickRow: function (rowIndex, rowData) {     //双击行弹框编辑
                ui_paradise_edit();
            } ,
            onSelect: function (rowIndex, rowData) {
                
                var $ui_paradise_layout = $("#ui_paradise_layout");
                var paradisetree = $ui_paradise_layout.layout("panel", "east");
                if (paradisetree.panel("options").collapsed) {   //判断是否展开
                    $ui_paradise_layout.layout("expand", "east");
                }
                if (oldParadiseId != rowData.id) {
                    $('#pardisetreeLoading').show();
                    oldParadiseId = rowData.id;
                    paradisetree.panel("setTitle", rowData.name + "结构");
                    $("#ui_paradise_tree").hide();
                    $("#pardisetreeLoading").show();
                    $("#ui_paradise_tree").tree({
                        url: 'ashx/bg_grave_ext.ashx?action=gettree',
                        lines: true,
                        formatter: function (node) {
                            var s = node.text;
                            if (node.children) {
                                s += '&nbsp;<span style=\'color:blue\'>(' + node.children.length + ')</span>';
                            }
                            return s;
                        },
                        onClick: function (node) {

                        },
                        queryParams: {
                            id:rowData.id,code:rowData.code
                        },
                        onBeforeExpand: function (node) {
                            $('#ui_paradise_tree').tree('options').queryParams = { id: node.id, code: node.code };
                        },
                        onLoadSuccess: function (node, data) {
                            $('#pardisetreeLoading').hide();
                            $("#ui_paradise_tree").show();
                        }
                    });
                }
               
            } 
        });        
        /*
        var ht = '<td><div id="spanhzyear"><span style="color:blue">年度:</span><select name="txthzyear" id="txthzyear" ></select></div></td>';
        $('.datagrid-toolbar table tr:last').append(ht);
        for (var i = 2000; i < 2050; i++) {
            $("#txthzyear").append("<option value='" + i + "'>" + i + "</option>");
        }
        var year = (new Date()).getFullYear();
        $("#txthzyear").val(year);
        */
    }

    function ui_paradise_add() {
        $("<div/>").dialog({
            id: "ui_paradise_add_dialog",
            href: "html/ui_paradise_edit.html",
            iconCls: "icon-add",
            title: "新增登骨灰堂",
            height:340,
            width: 600,
            modal: true,
            buttons: [{
                id: "ui_paradise_add_btn",
                text: '添加',
                iconCls: "icon-add",
                handler: function () {
                    $("#ui_paradise_editform").form("submit", {
                        url: "ashx/bg_paradise.ashx",
                        onSubmit: function (param) {
                            
                            $('#ui_paradise_add_btn').linkbutton('disable');    //点击就禁用按钮，防止连击
                            param.action = 'add';
                            if ($(this).form('validate')) {                                
                                return true;
                            }
                            else {
                                $('#ui_paradise_add_btn').linkbutton('enable');   //恢复按钮                                
                                return false;
                            }
                        },
                        success: function (data) {
                            var dataJson = JSON.parse(data);    //转成json格式
                            if (dataJson.success) {
                                $("#ui_paradise_add_dialog").dialog('destroy');  //销毁dialog对象
                                $.show_warning("提示", dataJson.msg);
                                $("#ui_paradise_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                            } else {
                                $('#ui_paradise_add_btn').linkbutton('enable');  //恢复按钮
                                $.show_warning("提示", dataJson.msg);
                            }
                        }
                    });
                }
            }],
            onLoad: function () {
                $("#ui_paradise_name").focus();
            },
            onClose: function () {
                $("#ui_paradise_add_dialog").dialog('destroy');  //销毁dialog对象
            }
        });
    }

 
    function ui_paradise_edit() {
        var rows = $("#ui_paradise_dg").datagrid("getChecked");
        if (rows.length < 1) {
            $.show_warning("提示", "请先勾选要修改的行或者双击某行");
            return;
        }
        if (rows.length > 1) {
            $.show_warning("提示", "不支持批量修改");
            $("#ui_paradise_dg").datagrid('clearSelections').datagrid('clearChecked');
            return;
        }
        var row = rows[0];
        $("<div/>").dialog({
            id: "ui_paradise_edit_dialog",
            href: "html/ui_paradise_edit.html",
            iconCls: "icon-edit",
            title: "修改骨灰堂信息",
            height:340,
            width: 600,
            modal: true,
            buttons: [{
                id: "ui_paradise_edit_btn",
                text: '修 改',
                iconCls: "icon-edit",
                handler: function () {
                    $("#ui_paradise_editform").form("submit", {
                        url: "ashx/bg_paradise.ashx",
                        onSubmit: function (param) {
                            $('#ui_paradise_edit_btn').linkbutton('disable');   //点击就禁用按钮，防止连击
                            param.action = 'edit';
                            if ($(this).form('validate')) {
                                return true;
                            }
                            else {
                                $('#ui_paradise_edit_btn').linkbutton('enable');   //恢复按钮
                                return false;
                            }
                            
                        },
                        success: function (data) {
                            var dataJson = JSON.parse(data);    //转成json格式
                            if (dataJson.success) {
                                $("#ui_paradise_edit_dialog").dialog('destroy');  //销毁dialog对象
                                $.show_warning("提示", dataJson.msg);
                                $("#ui_paradise_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                            } else {
                                $('#ui_paradise_edit_btn').linkbutton('enable');    //恢复按钮
                                $.show_warning("提示", dataJson.msg);
                            }
                        }
                    });
                }
            }],
            onLoad: function () {                
                $("#ui_paradise_id").val(row.id);                
                $("#ui_paradise_name").textbox('setValue', row.name);
                $("#ui_paradise_num").numberspinner('setValue', row.paradiseNum);
                $("#ui_paradise_code").val('setValue', row.code);
                $("#ui_paradise_fullName").textbox('setValue', row.fullName);
                $("#ui_paradise_type").combobox('setValue', row.type);
                $("#ui_paradise_areaId").combotree('setValue', row.areacode);
                $("#ui_paradise_pic").val(row.pic);
                $("#ui_paradise_pic_img").attr('src', row.pic + "?t=" + new Date().getTime());
                $("#ui_paradise_hourseNum").textbox('setValue', row.hourseNum);
                $("#ui_paradise_graves").numberbox('setValue', row.graves);
                $("#ui_paradise_otherName").textbox('setValue', row.otherName); 
                
            },
            onClose: function () {
                $("#ui_paradise_edit_dialog").dialog('destroy');  //销毁dialog对象
            }
        });
    }
    function ui_paradise_delete() {
        var rows = $("#ui_paradise_dg").datagrid("getChecked");
        if (rows.length < 1) {
            $.show_warning("提示", "请先勾选要删除的行");
            return;
        }
        $.messager.confirm('提示', '确定删除勾选的' + rows.length + '行？', function (r) {
            if (r) {
                para = {};
                para.action = "delete";
                para.timespan = new Date().getTime();
                para.id = "";
                var ids = [];
                $.each(rows, function (i, row) {
                    ids.push(row.id);
                });
                para.id = ids.join(",");
                $.ajax({
                    url: "ashx/bg_paradise.ashx",
                    data: para,
                    type: "POST",
                    dataType: "json",
                    success: function (data) {
                        if (data.success) {
                            $.show_warning("提示", data.msg);
                            $("#ui_paradise_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                        } else {
                            $.show_warning("提示", data.msg);
                        }
                    }
                });
            }
        });
    }

    function ui_paradise_init() {
        var rows = $("#ui_paradise_dg").datagrid("getChecked");
        if (rows.length < 1) {
            $.show_warning("提示", "请先勾选要操作的行！");
            return;
        }
        if (rows.length > 1) {
            $.show_warning("提示", "不支持批量初始化");
            $("#ui_paradise_dg").datagrid('clearSelections').datagrid('clearChecked');
            return;
        }
        var row = rows[0];
        var url = "html/ui_paradise_init.html";
        if (row.type != "骨灰堂") {
            url = "html/ui_paradise_init2.html";
        }

        $("<div/>").dialog({
            id: "ui_paradise_init_dialog",
            href: url,
            iconCls: "icon-edit",
            title: row.name+ "骨灰堂布局设置",
            height: 240,
            width: 500,
            modal: true,
            buttons: [{
                id: "ui_paradise_init_btn",
                text: '初始化',
                iconCls: "icon-edit",
                handler: function () {
                    $("#ui_paradise_initform").form("submit", {
                        url: "ashx/bg_paradise.ashx",
                        onSubmit: function (param) {
                            $('#ui_paradise_init_btn').linkbutton('disable');   //点击就禁用按钮，防止连击
                            param.action = 'init';
                            if ($(this).form('validate')) {
                                var res = confirm('初始化将删除骨灰堂原有的穴位及骨灰存放信息，您确定要进行初始化吗？');
                                if (!res) {
                                    $('#ui_paradise_edit_btn').linkbutton('enable');   //恢复按钮                                    
                                }
                                return res;
                            }
                            else {
                                $('#ui_paradise_init_btn').linkbutton('enable');   //恢复按钮
                                return false;
                            }
                        },
                        success: function (data) {
                            var dataJson = JSON.parse(data);    //转成json格式
                            if (dataJson.success) {
                                $("#ui_paradise_init_dialog").dialog('destroy');  //销毁dialog对象
                                $.show_warning("提示", dataJson.msg);
                                $("#ui_paradise_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                            } else {
                                $('#ui_paradise_init_btn').linkbutton('enable');    //恢复按钮
                                $.show_warning("提示", dataJson.msg);
                            }
                        }
                    });
                }
            }],
            onLoad: function () {
                $("#ui_paradise_id").val(row.id);
                $("#ui_paradise_hourseNum").textbox('setValue', row.hourseNum);
                //$("#ui_paradise_floorNum").numberbox('setValue', row.floors);
                //$("#ui_paradise_eastSections").numberbox('setValue', row.eastSections);
                //$("#ui_paradise_westSections").numberbox('setValue', row.westSections);
                //$("#ui_paradise_lines").numberbox('setValue', row.lines);
                //$("#ui_paradise_layers").numberbox('setValue', row.layers);
                //$("#ui_paradise_columns").numberbox('setValue', row.columns);
                //$("#ui_paradise_graves").numberbox('setValue', row.graves);
            },
            onClose: function () {
                $("#ui_paradise_init_dialog").dialog('destroy');  //销毁dialog对象
            }
        });
    }
    function ui_paradise_start() {
        var value = $('#ui_paradise_importform_p').progressbar('getValue');
        if (value < 95) {
            value += 5;
            $('#ui_paradise_importform_p').progressbar('setValue', value);
            setTimeout(arguments.callee, 1000);
        }
    };
    //穴位布局：上传导入穴位数据
    function ui_paradise_import() {

        $("<div/>").dialog({
            id: "ui_paradise_import_dialog",
            href: "html/ui_paradise_import.html",
            iconCls: "icon-filter",
            title: '数据导入',
            height: 220,
            width: 420,
            modal: true,
            buttons: [{
                id: "ui_paradise_import_btn",
                text: '布局导入',
                iconCls: "icon-import",
                handler: function () {
                    $("#ui_paradise_importform").form("submit", {
                        url: "ashx/bg_paradise.ashx",
                        onSubmit: function (param) {
                            $('#ui_paradise_import_btn').linkbutton('disable');    //点击就禁用按钮，防止连击
                            param.action = 'import';
                            param.op = 'import';
                            if ($(this).form('validate')) {
                                $('#ui_paradise_importform_p').progressbar('setValue', 0);
                                $('#ui_paradise_importform_msg').text("开始导入...");
                                ui_paradise_start();
                                return true;
                            }
                            else {
                                $('#ui_paradise_import_btn').linkbutton('enable');   //恢复按钮                                
                                return false;
                            }
                        },
                        success: function (data) {
                            var dataJson = JSON.parse(data);    //转成json格式
                            $('#ui_paradise_importform_p').progressbar('setValue', 100);
                            if (dataJson.success) {
                                $("#ui_paradise_import_dialog").dialog('destroy');  //销毁dialog对象
                                $.show_warning("提示", dataJson.msg);
                                $("#ui_paradise_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                            } else {
                                $('#ui_paradise_import_btn').linkbutton('enable');  //恢复按钮
                                $('#ui_paradise_importform_msg').html($('<div/>').html(dataJson.msg).text());
                                //$.show_warning("提示",  "导入失败！");
                            }
                        }
                    });
                }
            }],
            onLoad: function () {
            },
            onClose: function () {
                $("#ui_paradise_import_dialog").dialog('destroy');  //销毁dialog对象
            }
        });
    }

    function ui_paradise_setadmin() {
        var rows = $("#ui_paradise_dg").datagrid("getChecked");
        if (rows.length < 1) {
            $.show_warning("提示", "请先勾选要分配的行！");
            return;
        }
        if (rows.length > 1) {
            $.show_warning("提示", "不支持批量设置");
            $("#ui_paradise_dg").datagrid('clearSelections').datagrid('clearChecked');
            return;
        }
        var row = rows[0];
        $("<div/>").dialog({
            id: "ui_paradise_setadmin_dialog",
            href: "html/ui_paradise_setadmin.html",
            iconCls: "icon-edit",
            title: "设置管理员",
            height: 430,
            width: 620,
            modal: true,
            buttons: [{
                id: "ui_paradise_setadmin_btn",
                text: '设置',
                iconCls: "icon-edit",
                handler: function () {
                    $('#ui_paradise_setadmin_btn').linkbutton('disable');
                    var userIds = [];
                    var nodes = $('#ui_paradise_setadmin_user').combotree("tree").tree('getChecked');
                    for (var i = 0; i < nodes.length; i++) {
                        userIds.push(nodes[i].id);
                        //最终拼接示例：1,2,11 按钮id列表
                    }
                    var para = {};
                    para.action = "setadmin";
                    para.timespan = new Date().getTime();
                    para.userIds = userIds.join(',');
                    para.paradiseId = row.id;

                    $.ajax({
                        url: "ashx/bg_paradise.ashx",
                        data: para,
                        type: "POST",
                        dataType: "json",
                        success: function (data) {
                            if (data.success) {
                                $("#ui_paradise_setadmin_dialog").dialog('destroy');  //销毁dialog对象
                                $.show_warning("提示", data.msg);
                                $("#ui_paradise_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                            } else {
                                $('#ui_paradise_setadmin_btn').linkbutton('enable');
                                $.show_warning("提示", data.msg);
                            }
                        }
                    });
                }
            }],
            onLoad: function () {
                $("#ui_paradise_setadmin_paradise_id").val(row.id);
                $("#ui_paradise_setadmin_paradise_name").textbox('setValue', row.name);
                $("#ui_paradise_code").textbox('setValue', row.code);
                if (row.userIds) {
                    $('#ui_paradise_setadmin_user').combotree("setValues", row.userIds);
                }
            },
            onClose: function () {
                $("#ui_paradise_setadmin_dialog").dialog('destroy');  //销毁dialog对象
            }
        });
    }

    function ui_paradise_hzBase() {
        alert("hzBase");
    }

    function ui_paradise_bzMonth() {
        alert("bzMonth");
    }
    function ui_paradise_refresh() {
        var node = $('#ui_paradise_tree').tree('getSelected');

        var pn = $('#ui_paradise_tree').tree('getParent', node.target);
        if (pn == null) {
            $('#ui_paradise_tree').tree('reload', null);
        } else {
            $('#ui_paradise_tree').tree('reload', pn.target);
        }


    }
    function ui_paradise_expand() {
        var node = $('#ui_paradise_tree').tree('getSelected');
        $('#ui_paradise_tree').tree('expandAll', node.target);
    }
    function ui_paradise_collapse() {
        var node = $('#ui_paradise_tree').tree('getSelected');
        $('#ui_paradise_tree').tree('collapseAll', node.target);
    }
    function ui_paradise_showArch() {
        
        var rows = $("#ui_paradise_dg").datagrid("getChecked");
        if (rows.length != 1) {
            $.show_warning("提示", "请先选择一行！");
            return;
        }
        var row = rows[0];
        var $ui_paradise_layout = $("#ui_paradise_layout");
        var paradisetree = $ui_paradise_layout.layout("panel", "east");
        if (paradisetree.panel("options").collapsed) {   //判断是否展开
            $ui_paradise_layout.layout("expand", "east");
        }
        if (oldParadiseId != row.id) {
            $('#pardisetreeLoading').show();
            oldParadiseId = row.id;
            paradisetree.panel("setTitle", row.name + "布局");
            $("#ui_paradise_tree").hide();
            $("#pardisetreeLoading").show();
            $("#ui_paradise_tree").tree({
                url: 'ashx/bg_grave_ext.ashx?action=gettree',
                lines: true,
                method: 'get',
                animate: true,
                queryParams: {
                    id: rowData.id, code: rowData.code
                },
                formatter: function (node) {
                    var s = node.text;
                    if (node.children) {
                        s += '&nbsp;<span style=\'color:blue\'>(' + node.children.length + ')</span>';
                    }
                    return s;
                },
                onClick: function (node) {

                },
                onBeforeExpand: function (node) {
                    $('#ui_paradise_tree').tree('options').queryParams = { id: node.id, code: node.code };
                },
                onContextMenu: function (e, node) {
                    e.preventDefault();
                    $(this).tree('select', node.target);
                    $('#ui_paradise_mm').menu('show', {
                        left: e.pageX,
                        top: e.pageY
                    });
                },
                onLoadSuccess: function (node, data) {
                    $('#pardisetreeLoading').hide();
                    $("#ui_paradise_tree").show();
                }
            });
        }
    }
</script>
<div id="ui_paradise_layout" class="easyui-layout" data-options="fit:true,border:false">       
    <div data-options="region:'center',border:false">            
        <table id="ui_paradise_dg" data-options="fit:true,border:false">
        </table>
    </div>
    <div data-options="region:'east',split:true,border:true,collapsed:true" style="width:15%;" title="骨灰堂布局" >
        <div id="pardisetreeLoading">正在加载数据，请稍候...</div>
        <ul id="ui_paradise_tree" data-options="fit:true,border:false"></ul>
        <div id="ui_paradise_mm" class="easyui-menu" style="width:120px;">
            <div onclick="ui_paradise_refresh()" data-options="iconCls:'icon-refresh'">刷新</div>
            <div class="menu-sep"></div>
            <div onclick="ui_paradise_expand()" data-options="iconCls:'icon-expand'">展开</div>
            <div onclick="ui_paradise_collapse()" data-options="iconCls:'icon-collapse'">收起</div>
        </div>
    </div>
</div>
